<?php
/**
 ***********************************************************************************************************************
 * 支付宝签名，验签工具
 * @author ysp
 ***********************************************************************************************************************
 */
namespace util\alipay;
class AlipaySignUtil {
    /**
     * 服务窗签名验证（成为开发者）
     * @return boolean
     */
    public static function verify($data, $sign,$alipay_public_key_path) {
        if(is_array($data)){
            $signData = self::_getSignContent($data);
        }else{
            $signData = $data;
        }

        /*读取公钥文件，PEM格式*/
        $pubKey = file_get_contents($alipay_public_key_path);

        /*转换为openssl格式密钥*/
        $res = openssl_get_publickey($pubKey);

        /*调用openssl内置方法验签*/
        $result = (bool) openssl_verify($signData,base64_decode($sign),$res);

        /*释放资源*/
        openssl_free_key($res);

        if($result)
            return true;
        else
            return false;
    }

    /**
     * 对数据进行签名
     * @param $data
     * @param $private_key_path
     */
    public static function sign($data,$private_key_path){
        if(is_array($data)){
            $signData = self::_getSignContent($data);
        }else{
            $signData = $data;
        }

        /*读取私钥文件，PEM格式*/
        $priKey = file_get_contents($private_key_path);

        /*转换为openssl格式密钥*/
        $res = openssl_get_privatekey($priKey);

        /*调用openssl内置方法对数据进行签名*/
        openssl_sign($signData,$sign,$res);

        /*释放资源*/
        openssl_free_key($res);

        /*对签名进行Base64编码，变为可读的字符串*/
        $sign = base64_encode($sign);

        return $sign;
    }

    /**
     * 获取服务窗公钥内容
     * @return mixed|string
     */
    public function getPublicKeyStr($public_key_path) {
        $content = file_get_contents ( $public_key_path );
        $content = str_replace ( "-----BEGIN PUBLIC KEY-----", "", $content );
        $content = str_replace ( "-----END PUBLIC KEY-----", "", $content );
        $content = str_replace ( "\r", "", $content );
        $content = str_replace ( "\n", "", $content );
        return $content;
    }

    /**
     * 获取13位的时间戳
     * @return int
     */
    public function getTimeStamp(){
        return round(microtime(true)*1000);
    }

    /**
     * 获取签名数据
     * @param $signData
     * @return string
     */
    private static function _getSignContent($signData){
        ksort($signData);

        $signContent = "";
        $i = 0;
        foreach ($signData as $k => $v) {
            if (false === self::_checkEmpty($v) && "@" != substr($v, 0, 1)) {
                if ($i == 0) {
                    $signContent .= "$k" . "=" . "$v";
                } else {
                    $signContent .= "&" . "$k" . "=" . "$v";
                }
                $i++;
            }
        }
        unset ($k, $v);
        return $signContent;
    }

    /**
     * 验证数据是否为空
     * @param $data
     * @return bool
     */
    private static function _checkEmpty($data){
        if(!isset($data))
            return true ;
        if($data === null )
            return true;
        if(trim($data) === "")
            return true;

        return false;
    }
}